package com.icacoo.cp;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.icacoo.cp.business.AutoSelectNumber;

public class MainActivity extends FragmentActivity {

	private static final int RED_BALL_NUMBER = 33;
	private static final int BULE_BALL_NUMBER = 16;
	

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;
	
	List<Integer> reds = new ArrayList<Integer>();
	List<Integer> blues = new ArrayList<Integer>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// Create the adapter that will return a fragment for each of the three
		// primary sections
		// of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		int count = mSectionsPagerAdapter.getCount();
		Fragment fragment = mSectionsPagerAdapter.getItem(0);

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the primary sections of the app.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			Fragment fragment = new DummySectionFragment();
			Bundle args = new Bundle();
			args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
			fragment.setArguments(args);
			return fragment;
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase();
			case 1:
				return getString(R.string.title_section2).toUpperCase();
			case 2:
				return getString(R.string.title_section3).toUpperCase();
			}
			return null;
		}
	}

	/**
	 * A dummy fragment representing a section of the app, but that simply
	 * displays dummy text.
	 */
	public static class DummySectionFragment extends Fragment {
		private static final int BLUE_PREFIX = 1100;
		private static final int RED_PREFIX = 1000;
		private static final int RECOMMEND_PREFIX = 100;

		public DummySectionFragment() {
		}

		public static final String ARG_SECTION_NUMBER = "section_number";

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			LinearLayout layout = new LinearLayout(getActivity());
			layout.setOrientation(LinearLayout.VERTICAL);
			addRecommend(layout);

			addSelectRedTitle(layout);

			addSelectRed(layout);

			addSelectRedBlue(layout);
			
			addSelectBlue(layout);
			
			addSelectButton(layout);
			
			return layout;
		}

		private void addSelectButton(LinearLayout layout) {
			LinearLayout btnLayout = new LinearLayout(getActivity());
			Button btn = new Button(getActivity());
			btn.setText(R.string.select_btn_title);
			btn.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					//TODO add click event
					Toast.makeText(getActivity(), getResources().getString(R.string.select_btn_title), Toast.LENGTH_SHORT).show();
					
					//TODO clear all textview status
					
					AutoSelectNumber select = new AutoSelectNumber();
					int reds[] = select.getReds();
					int blue = select.getBlue();
					
					FragmentActivity factivity = getActivity();
					for(int i=0;i<reds.length;i++){
						TextView text = (TextView)factivity.findViewById(reds[i]+RED_PREFIX);
						text.setTextColor(getResources().getColor(R.color.red));
					}
					TextView text = (TextView)factivity.findViewById(blue+BLUE_PREFIX);
					text.setTextColor(getResources().getColor(R.color.red));
				}
			});
			btnLayout.addView(btn);
			
			Button addToListBtn = new Button(getActivity());
			addToListBtn.setText(R.string.select_btn_addtolist);
			addToListBtn.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					//TODO add click event
					Toast.makeText(getActivity(), getResources().getString(R.string.select_btn_addtolist), Toast.LENGTH_SHORT).show();
				}
			});
			btnLayout.addView(addToListBtn);
			
			
			layout.addView(btnLayout);
		}

		private void addRecommend(LinearLayout layout) {
			LinearLayout topLayout = new LinearLayout(getActivity());
			// TODO calcution 添加 当天推荐的号码
			AutoSelectNumber select = new AutoSelectNumber();
			int reds[] = select.getReds();
			int blue = select.getBlue();

			TextView name = new TextView(getActivity());
			name.setText(R.string.name_tuijian);
			topLayout.addView(name);
			for (int i = 0; i < reds.length; i++) {
				topLayout.addView(createNumberView(""+reds[i],BallType.RECOMMEND));
			}
			topLayout.addView(createNumberView(""+blue,BallType.RECOMMEND));
			layout.addView(topLayout);
		}

		private void addSelectRed(LinearLayout layout) {
			LinearLayout inner = new LinearLayout(getActivity());
			for (int i = 1; i <= RED_BALL_NUMBER; i++) {
				inner.addView(createNumberView(""+i,BallType.RED));
				if ((i % 9) == 0) {
					layout.addView(inner);
					inner = new LinearLayout(getActivity());
				}
			}
			layout.addView(inner);
		}

		private void addSelectRedTitle(LinearLayout layout) {
			// 添加选红球标题
			LinearLayout selectRedLayout = new LinearLayout(getActivity());
			TextView redTitle = new TextView(getActivity());
			redTitle.setText(R.string.select_red);
			selectRedLayout.addView(redTitle);
			layout.addView(selectRedLayout);
		}
 
		private void addSelectRedBlue(LinearLayout layout) {
			// 添加选蓝球标题
			LinearLayout selectRedLayout = new LinearLayout(getActivity());
			TextView redTitle = new TextView(getActivity());
			redTitle.setText(R.string.select_blue);
			selectRedLayout.addView(redTitle);
		
			layout.addView(selectRedLayout);
		}

		private void addSelectBlue(LinearLayout layout) {
			LinearLayout inner = new LinearLayout(getActivity());
			for (int i = 1; i <= BULE_BALL_NUMBER; i++) {
				inner.addView(createNumberView(""+i,BallType.BLUE));
				if ((i % 9) == 0) {
					layout.addView(inner);
					inner = new LinearLayout(getActivity());
				}
			}
			layout.addView(inner);
		}
		private TextView createNumberView(String s,BallType type) {
			TextView textView = new TextView(getActivity());
			if(type.equals(BallType.RECOMMEND)){
				textView.setId(Integer.valueOf(s)+RECOMMEND_PREFIX);	
			}else if(type.equals(BallType.RED)){
				textView.setId(Integer.valueOf(s)+RED_PREFIX);
			}else if(type.equals(BallType.BLUE)){
				textView.setId(Integer.valueOf(s)+BLUE_PREFIX);
			}
			textView.setText(s);
			textView.setGravity(Gravity.CENTER);
			textView.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.number_bg));
			textView.setClickable(true);
			textView.setFocusable(true);
			textView.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TextView text = (TextView)v;
					text.setTextColor(getResources().getColor(R.color.red));
					text.setSelected(true);
					Toast.makeText(getActivity(), ""+text.getId()+" "+text.getText(), Toast.LENGTH_SHORT).show();
				}
			});
			getActivity().findViewById(textView.getId());
			return textView;
		}
	}
	enum BallType{
		RECOMMEND,RED,BLUE
	}

}
